<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="">
    
    <title>首页</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<meta http-equiv="content-type" content="text/html;charset=utf-8">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
	<script type="text/javascript" src="../../js/jquery-3.3.1.js"></script>
	<script type="text/javascript" src="../../js/bootstrap.min.js"></script>
	<link href="../../css/bootstrap.min.css" rel="stylesheet">
  </head>
  
  <body>
  <script type="text/javascript">
  	$(document).ready(function(){
  		$.ajax({url:"${pageContext.request.contextPath}/student/findAll",
  		dataType:"json",
  		type:"post",
  		success:function(data, status){
  			$("#m").append("<option selected='selected'>&emsp;&emsp;</option>");
  			for(var i = 0; i < data.data.length; i++)
  			{
  				$("#t").append("<tr>");
  				$("#t").append("<td>"+data.data[i].sname+"</td>"); 
  				$("#t").append("<td><button type='button' class='btn btn-sm' onclick='loop(value)' value='"+data.data[i].sid+"'>详细</button></td>");
  				$("#t").append("</tr>");
  			}
  		}});
  		$.ajax({url:"${pageContext.request.contextPath}/college/findAll",
  		dataType:"json",
  		type:"post",
  		success:function(data, status){
  			for(var i = 0; i < data.data.length; i++)
  			{
  				$("#c").append("<option value="+data.data[i].eid+">"+data.data[i].ename+"</option>");
  			}
  		}});
  	});
  	
  	function loop(value){
  		window.parent.right.location.href='detail.jsp?sid='+value;
  	}
  	
  	function ajaxMajorFindAll(value){
  		$.ajax({
  		url:"${pageContext.request.contextPath}/major/findAll",
  		dataType:"json",
  		type:"post",
  		data:{eid:value},
  		success:function(data, status){
  			$("#m").empty();
  			$("#m").append("<option selected='selected'>&emsp;&emsp;</option>");
  			for(var i = 0; i < data.data.length; i++)
  			{
  				$("#m").append("<option value="+data.data[i].mid+">"+data.data[i].mname+"</option>");
  			}	
  		}});
  	}
  	
  	function ajaxTeacherFindByMid(value){
  		$.ajax({
  			url:"${pageContext.request.contextPath}/student/findByMid",
  			dataType:"json",
  			type:"post",
  			data:{mid:value},
  			success:function(data, status){
  				$("#t").empty();
  				for(var i = 0; i < data.data.length; i++)
  				{
  					$("#t").append("<tr>");
  					$("#t").append("<td>"+data.data[i].sname+"</td>"); 
  					$("#t").append("<td><button type='button' class='btn btn-sm' onclick='loop(value)' value='"+data.data[i].sid+"'>详细</button></td>");
  					$("#t").append("</tr>");
  				}
  		}});
  	}
  	
  	function ajaxTeacherFindByFuzzy(){
  		$.ajax({
  			url:"${pageContext.request.contextPath}/student/findByFuzzy",
  			dataType:"json",
  			type:"post",
  			data:{sname:$("#f").val()},
  			success:function(data, status){
  			$("#t").empty();
  			if(data.code == 0)
  			{
  				alert(data.data);
  			}else if (data.code == 1){
 				for(var i = 0; i < data.data.length; i++)
  				{
  					$("#t").append("<tr>");
  					$("#t").append("<td>"+data.data[i].sname+"</td>"); 
  					$("#t").append("<td><button type='button' class='btn btn-sm' onclick='loop(value)' value='"+data.data[i].sid+"'>详细</button></td>");
  					$("#t").append("</tr>");
  				}
  			}
  		}});
  	}
  	
    function findMajor(value){
  		$.ajax({
  		url:"${pageContext.request.contextPath}/major/findAll",
  		dataType:"json",
  		type:"post",
  		data:{eid:value},
  		success:function(data, status){
  			$("#tm").empty();
  			$("#tm").append("<option selected='selected'>&emsp;&emsp;</option>");
  			for(var i = 0; i < data.data.length; i++)
  			{
  				$("#tm").append("<option value="+data.data[i].mid+">"+data.data[i].mname+"</option>");
  			}	
  		}});
  	}
  	
  	function findCollege(){
  	  	$.ajax({url:"${pageContext.request.contextPath}/college/findAll",
  		dataType:"json",
  		type:"post",
  		success:function(data, status){
  			for(var i = 0; i < data.data.length; i++)
  			{
  				$("#tc").append("<option value="+data.data[i].eid+">"+data.data[i].ename+"</option>");
  			}
  		}});
  	}
  	
  	function addStudent(){
  			$.ajax({
  			url:"${pageContext.request.contextPath}/student/add",
  			dataType:"json",
  			type:"post",
  			data:{susername:$("#tu").val(), spassword:$("#tp").val(), sname:$("#tn").val(), mid:$("#tm").val()},
  			success:function(data, status){
  			if(data.code == 0)
  			{
  				alert(data.message);
  			}else if (data.code == 1){
 				alert("添加成功");
 				window.location.href='studentlist.jsp';
  			}
  		}});
  	}
  </script>
  
<div class="container">
	<div class="row clearfix">
		<div class="col-md-12 column">
			<h3 class="text-center">
				学生
			</h3>
			<br/>
			<div class="col-md-6 column">
			 <button type="button" class="btn btn-sm" data-toggle="modal" data-target="#myModal" onclick="findCollege()">添加</button>
			 &emsp;
			 <span>
			 	学院&nbsp;<select id = "c" onchange="ajaxMajorFindAll(this.value)">
			 		<option selected="selected"></option>
				</select>
			 </span>
			 &emsp;
			 <span>
			 	专业&nbsp;<select id = "m" onchange="ajaxTeacherFindByMid(this.value)">
				</select>
			 </span>
			</div>
			<div class="col-md-6 column">
			&emsp;&emsp;&emsp;&emsp;
			<span>
				用户名&nbsp;<input type="text" id="f">
			 </span>
			 <span>
				<input type="button" id="find" value="查找" onclick="ajaxTeacherFindByFuzzy()">
			 </span>
			</div>
			<br/>
			<table class="table table-bordered table-hover">
				<thead>
					<tr>
						<th>
							学生
						</th>
						<th>
							菜单
						</th>
					</tr>
				</thead>
				<tbody id="t">
			
				</tbody>
			</table>
		</div>
	</div>
</div>
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h4 class="modal-title" id="myModalLabel">
                  	添加学生
                </h4>
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">
                    &times;
                </button>
            </div>
            <form id="form_data">
            <div class="modal-body">
                                                         用户名: <input type="text" id="tu"/><br/><br/>
                                                         密&emsp;码: <input type="text" id="tp"/><br/><br/>
                                                         姓&emsp;名: <input type="text" id="tn"/><br/><br/>
                                                         学&emsp;院: <select id = "tc" onchange="findMajor(this.value)">
			 		     <option selected="selected"></option>
					  </select><br/><br/>
                                                         专&emsp;业: <select id = "tm">
				</select>                                                                                 
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭
                </button>
                <button type="button" onclick="addStudent()" class="btn btn-primary">
                   	添加
                </button>
            </div>
            </form>
        </div><!-- /.modal-content -->
    </div><!-- /.modal -->
</div>
  </body>
</html>
